﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static KongMing.ORM.SqlSugar.SqlSugarHelper;
using KongMing.ORM.SqlSugar.Models;

namespace KongMing.ORM.SqlSugar
{
    public class Json字段操作
    {
        public static void Go()
        {
            using (var db = GetDB())
            {
                //1.新增数据
                SqlSugarJsonConfig newConfig = new SqlSugarJsonConfig();
                newConfig.Key = "sqlsugar";
                newConfig.Name = "sqlsugar配置";

                var strConfigs = new List<Config_String>();
                strConfigs.Add(new Config_String() { Key = "sqlsugar_stringkey_key1", Value = "sqlsugar_stringvalue_value1" });
                strConfigs.Add(new Config_String() { Key = "sqlsugar_stringkey_key2", Value = "sqlsugar_stringvalue_value2" });
                newConfig.StringConfig = strConfigs;

                var intConfig = new Config_Int()
                {
                    Key = "sqlsugar_intkey_key1",
                    Value = 3,
                    MaxValue = 10,
                    MinValue = -10
                };
                newConfig.IntConfig = intConfig;

                int i = db.Insertable(newConfig).ExecuteCommand();


                //2.查询
                var strConfig = db.Queryable<SqlSugarJsonConfig>().InSingle("sqlsugar")?.StringConfig
                    ?.Where(y => y.Key == "sqlsugar_stringkey_key1")?.FirstOrDefault()?.Value ?? "";

                //2.修改数据
                var config = db.Queryable<SqlSugarJsonConfig>().InSingle("sqlsugar");
                config.IntConfig.MaxValue = 20;
                int j = db.Updateable(config).ExecuteCommand();
            }
        }
    }
}
